package com.loren.tetris.windows.ui;

import com.loren.tetris.data.GameData;
import com.loren.tetris.data.constant.DataConstant;
import com.loren.tetris.data.entity.Block;
import com.loren.tetris.data.entity.BlockShape;

import java.awt.*;
import java.util.List;
import java.util.stream.Collectors;

public class BoxNext extends Box {

    public BoxNext(int boxX, int boxY, int boxW, int boxH, GameData gameData) {
        super(boxX, boxY, boxW, boxH, gameData);
    }

    @Override
    protected void gameAction(Graphics g) {
        if (this.getGameData().isGameOver()) {
            return;
        }
        BlockShape next = this.getGameData().getNext();
        // 需要微调计算 使得下一个方块显示在合适的位置
        int minX = this.getMinValue(next.getBlockList().stream().map(Block::getX).collect(Collectors.toList()));
        int minY = this.getMinValue(next.getBlockList().stream().map(Block::getY).collect(Collectors.toList()));
        for (Block block : next.getBlockList()) {
            int x = this.getBoxX() + (block.getX() - minX) * DataConstant.BLOCK_NEXT_SIZE + 20;
            int y = this.getBoxY() + (block.getY() - minY) * DataConstant.BLOCK_NEXT_SIZE + 18;
            g.setColor(block.getColor());
            g.drawRoundRect(x, y, DataConstant.BLOCK_NEXT_SIZE, DataConstant.BLOCK_NEXT_SIZE, DataConstant.BLOCK_ROUNDNESS, DataConstant.BLOCK_ROUNDNESS);
        }
    }

    private Integer getMinValue(List<Integer> intList) {
        return intList.stream().distinct().min(Integer::compareTo).orElse(0);
    }
}
